/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.addressBook.controller;

import com.addressBook.bean.Groups;
import com.addressBook.bean.UserInfo;
import com.addressBook.tools.CommonPhotoTools;
import com.addressBook.util.AllUtil;
import com.fxexperience.javafx.animation.FlipInYTransition;
import com.fxexperience.javafx.animation.FlipOutYTransition;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Random;
import java.util.ResourceBundle;
import javafx.animation.Transition;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

/**
 * FXML Controller class
 *
 * @author Administrator
 */
public class Group2listController implements Initializable {

    private static Group2listController instance;

    public static Group2listController getGroup2listController() {
        return instance;
    }
    @FXML
    private ImageView back;
    @FXML
    private VBox pageBox;
    @FXML
    private HBox personInfoBox1;
    @FXML
    private HBox personInfoBox2;
    @FXML
    private HBox personInfoBox3;
    @FXML
    AnchorPane groud2list;
    @FXML
    private Label text;
//    @FXML
//    public AnchorPane personInfoPanel;

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        instance = this;
        back.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                GroupController gc = GroupController.getGroupController();
                Transition trans = new FlipOutYTransition(gc.group2list);
                trans.setOnFinished(new EventHandler<ActionEvent>() {

                    @Override
                    public void handle(ActionEvent event) {
                        gc.group2list.setVisible(false);
                        gc.back();
                    }
                });
                trans.play();

            }
        });
    }

    public void showUser(Groups g, ArrayList<UserInfo> list) {
        int nowpage = 1, page = 0;//nowpage当前页面，page为页面数
        ArrayList<String> colorTable = new ArrayList<String>() {
            {
                add("#008287");
                add("#16499a");
                add("#4390df");
                add("#60a917");
                add("#fa6800");
                add("#9a1616");
            }
        };
//        System.out.println(g.getGroupName());
//        System.out.println("dd");
        if (g.getGroupName().equals("")) {
            text.setText("默认");
        } else {
            text.setText(g.getGroupName());
        }
//        System.out.println("dadflka");
        if (list != null) {
            page = list.size() / 8;
            if (list.size() % 8 != 0) {
                page = page + 1;
            }
            personInfoBox1.getChildren().clear();
            personInfoBox2.getChildren().clear();
            personInfoBox3.getChildren().clear();

            int k = (nowpage - 1) * 8;
            int max = nowpage * 8;
            if (page == nowpage) {
                max = list.size();
            }
            for (int i = 0; i < 8; i++) {
                UserInfo userBean = list.get(k);
                AnchorPane pane = new AnchorPane();
                pane.setPrefSize(260, 100);
                ImageView personview = new ImageView();
                byte[] buf = CommonPhotoTools.Str2byte(userBean.getPHOTO());
                InputStream is = new ByteArrayInputStream(buf);
                Image image = new Image(is);
                personview.setImage(image);
                personview.setFitHeight(100);
                personview.setFitWidth(100);
                personview.setLayoutX(5);
                personview.setLayoutY(10);
                Label nameLabel = new Label(userBean.getFN());
                nameLabel.setPrefSize(100, 100);
                nameLabel.setLayoutX(120);
                nameLabel.setLayoutY(20);
                nameLabel.setStyle("-fx-text-fill:white;");
                pane.getChildren().addAll(personview, nameLabel);
                int color = new Random().nextInt(5);
                pane.setStyle("-fx-background-color:" + colorTable.get(color) + ";");
                pane.setOnMouseClicked(new EventHandler<MouseEvent>() {

                    @Override
                    public void handle(MouseEvent event) {
                        AllUtil.personBean = userBean;
                        GroupController gc=GroupController.getGroupController();
                        Group2listController g2c=Group2listController.getGroup2listController();
                        g2c.groud2list.setVisible(false);
                        gc.groupPane.setVisible(false);
                        
//                        back.setVisible(false);
//                        text.setVisible(false);
//                        pageBox.setVisible(false);
//                        leftButton.setVisible(false);
//                        rightButton.setVisible(false);
//                        PersonInfoController pc=PersonInfoController.getPersonInfoController();
                        UIController uc=UIController.getUIController();
                        uc.personInfoPanel.setOpacity(0);
//                        toolPanelContainer.setVisible(true);
                        uc.personInfoPanel.setVisible(true);
//                        uc.;
                        new FlipInYTransition(uc.personInfoPanel).play();
                        PersonInfoController pc = PersonInfoController.getPersonInfoController();
                        pc.setText();
                    }
                });
                if (i <= 2) {
                    personInfoBox1.getChildren().add(pane);
                    k = k + 1;
                } else if (i <= 5) {
                    personInfoBox2.getChildren().add(pane);
                    k = k + 1;
                } else if (i <= 7) {
                    personInfoBox3.getChildren().add(pane);
                    k = k + 1;
                }
                if (k >= max) {
                    AnchorPane addPane = new AnchorPane();
                    addPane.setPrefSize(260, 100);
                    Label addLabel = new Label("新增");
//                addLabel.setLayoutX(100);
//                addLabel.setLayoutY(80);
//                Image addImage=new Image("icons/useradd.png");
//                ImageView addImageView=new ImageView(addImage);
//                addImageView.setFitHeight(80);
//                addImageView.setFitWidth(80);
//                addImageView.setLayoutX(90);
//                addImageView.setLayoutY(0);
                    addLabel.setStyle("-fx-text-fill:white;");
                    addPane.getChildren().add(addLabel);
                    color = new Random().nextInt(5);
                    addPane.setStyle("-fx-background-color:" + colorTable.get(color) + ";");
//                addPane.setOnMouseClicked(new EventHandler<MouseEvent>() {
//
//                    @Override
//                    public void handle(MouseEvent event) {
//                        AllUtil.personBean = new UserInfo();
//                        toolBar.setVisible(false);
//                        iconBar.setVisible(false);
//                        pageBox.setVisible(false);
//                        leftButton.setVisible(false);
//                        rightButton.setVisible(false);
//                        personInfoPanel.setOpacity(0);
//                        toolPanelContainer.setVisible(true);
//                        personInfoPanel.setVisible(true);
//                        new FlipInYTransition(personInfoPanel).play();
//                        PersonInfoController pc = PersonInfoController.getPersonInfoController();
//                        pc.setText();
//                    }
//                });
                    if (i < 2) {
                        personInfoBox1.getChildren().add(addPane);
                        k = k + 1;
                    } else if (i < 5) {
                        personInfoBox2.getChildren().add(addPane);
                        k = k + 1;
                    } else if (i < 8) {
                        personInfoBox3.getChildren().add(addPane);
                        k = k + 1;
                    }
                    break;
                }
            }
        }
    }

}
